﻿Imports Aricie.DNN.Modules.LuceneSearch.Business

Namespace Components.Lucene.ExistingIndex
    Public Class ExistingIndexStructure(Of T As {New, ExistingIndexDocument})

        Protected Friend UidToDocument As Dictionary(Of String, T)
        Protected Friend DuplicateEntries As New HashSet(Of String)

        Public Sub New(LQR As LuceneQueryResults)
            ' we assume that there are no duplicates in the lucene index, so let's build our UidToDocument dictionary with the right size immediately
            UidToDocument = New Dictionary(Of String, T)(LQR.NbResults)

            ' turn query results into the needed class
            ' we need to load only a small subset of the fields, so we need to use a SetBasedFieldSelector
            Dim FieldsNeeded As New HashSet(Of String)

            FieldsNeeded.Add(LuceneGeneralField.Uid)
            FieldsNeeded.Add(LuceneGeneralField.HashCode)
            FieldsNeeded.Add(LuceneGeneralField.IndexedDate)
            FieldsNeeded.Add(LuceneGeneralField.ModifiedDate)
            FieldsNeeded.Add(DnnSearchItemField.PubDate)
            FieldsNeeded.Add(LuceneGeneralField.PubHistory)

            Dim Selecteur As Global.Lucene.Net.Documents.SetBasedFieldSelector = SpecificVersionLuceneProvider.Instance().GetSetBaseFieldSelectorFromHasSet(FieldsNeeded)

            LQR.ActionOnEachDocumentInResults(AddressOf CreateCurrentExistingIndexDocument, Selecteur)

        End Sub

        Private Sub CreateCurrentExistingIndexDocument(CurrentDocument As Global.Lucene.Net.Documents.Document)
            Dim NewExistingIndexDocument As New T
            NewExistingIndexDocument.Uid = CurrentDocument.Get(LuceneGeneralField.Uid)
            NewExistingIndexDocument.Hashcode = CurrentDocument.Get(LuceneGeneralField.HashCode)
            NewExistingIndexDocument.IndexedDate = GetDateFromFieldValue(CurrentDocument.Get(LuceneGeneralField.IndexedDate), DateTime.MinValue)
            NewExistingIndexDocument.ModifiedDate = GetDateFromFieldValue(CurrentDocument.Get(LuceneGeneralField.ModifiedDate), NewExistingIndexDocument.IndexedDate)
            NewExistingIndexDocument.PublicationDate = GetDateFromFieldValue(CurrentDocument.Get(DnnSearchItemField.PubDate), NewExistingIndexDocument.ModifiedDate)
            NewExistingIndexDocument.PublicationHistory = CurrentDocument.Get(LuceneGeneralField.PubHistory)


            ' let's add this document to the dictionary we already got
            If UidToDocument.ContainsKey(NewExistingIndexDocument.Uid) Then
                ' we have a duplicate entry, so every document with this id must be marked: we also remove it from the dictionary
                DuplicateEntries.Add(NewExistingIndexDocument.Uid)
                UidToDocument.Remove(NewExistingIndexDocument.Uid)
            Else
                UidToDocument(NewExistingIndexDocument.Uid) = NewExistingIndexDocument
            End If

            For Each id In DuplicateEntries
                UidToDocument.Remove(id)
            Next
        End Sub

        Private Function GetDateFromFieldValue(dateValue As String, DefaultDateTime As DateTime) As DateTime
            If (String.IsNullOrEmpty(dateValue)) Then
                Return DefaultDateTime
            End If
            Return Global.Lucene.Net.Documents.DateTools.StringToDate(dateValue)
        End Function

    End Class
End Namespace